﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using NLog;
using OddUserManager.Model;
using OddUserManager.Service.IServices;
using OddUserManager.Web.Models;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace OddUserManager.Web.Controllers
{
    public class SiteController : Controller
    {
        static Logger log = LogManager.GetCurrentClassLogger();

        ISiteService _service;
        public SiteController(ISiteService service)
        {
            _service = service;
        }
        public IActionResult GetAllSites()
        {
            JsonData d = new JsonData();

            d.Data = _service.GetAllList().OrderBy(p => p.Name).ToList();

            return Json(d);
        }


        public IActionResult GetSitesByParentId(int id,int startPage,int pageSize)
        {
            JsonData d = new JsonData();

            var list = _service.GetAllList().Where(p => p.ParentId == id).OrderBy(p => p.Name).ToList();
            if (list.Count == 0)
            {
                d.Data = new PageCollection
                {
                    PageIndex = startPage,
                    PageSize = pageSize,
                    AllCount = 0,
                    PageCount = 0,
                    Data = list
                };
            }
            else
            {
                d.Data = new PageCollection
                {
                    PageIndex = startPage,
                    PageSize = pageSize,
                    AllCount = list.Count,
                    PageCount = Math.Ceiling(Convert.ToDecimal(list.Count) / pageSize),
                    Data = list.Skip((startPage - 1) * pageSize).Take(pageSize).ToList()
                };
            }
            return Json(d);
        }


        public IActionResult GetSiteTreeData() {
            var menus = _service.GetAllList().OrderBy(p=>p.SortValue).ThenBy(p=>p.Name).ToList();
            List<TreeModel> treeModels = new List<TreeModel>();
            foreach (var menu in menus)
            {
                treeModels.Add(new TreeModel() { Id = menu.Id.ToString(), Text = menu.Name, Parent = menu.ParentId<=0?"#":menu.ParentId.ToString()  });
            }
            JsonData data = new JsonData();
            data.Data = treeModels;
            return Json(data);
        }


        public IActionResult Get(int id)
        {
            JsonData d = new JsonData();
            d.Success = true;
            d.Data = _service.Get(id);
            return Json(d);
        }

        public IActionResult Edit(Site dto)
        {
            JsonData d = new JsonData();
            try
            {
                var oldSite = _service.Get(dto.Id);
                var site = _service.Get(dto.ParentId);
                dto.LeftValue = site.RightValue;
                dto.RightValue = dto.LeftValue + 1;
                 _service.UpdateOrInsert(dto);
                
            }
            catch (Exception ex)
            {
                d.Success = false;
                d.Message = ex.Message;
            }
            return Json(d);
        }

        public IActionResult DeleteMuti(string ids)
        {
            JsonData d = new JsonData();
            try
            {
                string[] idArray = ids.Split(',');

                var list = _service.GetAllList();
                List<int> delIds = new List<int>();
                foreach (string id in idArray)
                {
                    if (list.Count(p => p.ParentId ==int.Parse(id)) > 0)
                    {
                        throw new Exception("站点下面有子站点");
                    }

                    delIds.Add(int.Parse(id));
                }
                _service.DeleteBatch(delIds);
               
            }
            catch (Exception ex)
            {
                d.Success = false;
                d.Message = ex.Message;
            }
            return Json(d);
        }

        public IActionResult GetNoneChildrenSites() {
            JsonData d = new JsonData();
            d.Data = _service.GetNoneChildrenSites();
            return Json(d);
        }



        // GET: /<controller>/
        public IActionResult Index()
        {
            return View();
        }
    }
}
